#include <QCoreApplication>
#include <QDebug>
#include <QTimer>
#include <QDir>
#include <QJsonObject>
#include <QJsonDocument>
#include <QDateTime>
#include <QTcpSocket>
#include <QTcpServer>
#include <QFile>

// 创建一个简化版的机器人管理器，不依赖数据库
class SimpleRobotManager : public QObject
{
    Q_OBJECT

public:
    explicit SimpleRobotManager(QObject *parent = nullptr) : QObject(parent) {
        // 初始化控制命令日志文件
        QString logDir = QCoreApplication::applicationDirPath() + "/logs";
        QDir dir(logDir);
        if (!dir.exists()) {
            dir.mkpath(logDir);
        }
        
        QString logFileName = logDir + "/control_commands.log";
        m_controlLog.setFileName(logFileName);
    }
    
    bool sendControlCommand(const QString &sn, const QVariantMap &command) {
        // 将QVariantMap转换为QJsonObject
        QJsonObject jsonCommand;
        for (auto it = command.begin(); it != command.end(); ++it) {
            jsonCommand[it.key()] = QJsonValue::fromVariant(it.value());
        }
        
        // 记录日志
        logControlCommand(sn, jsonCommand);
        
        qDebug() << QString("模拟发送控制命令到机器人 %1: %2").arg(sn).arg(
            QString(QJsonDocument(jsonCommand).toJson(QJsonDocument::Compact)));
        
        // 模拟发送成功
        return true;
    }

private:
    QFile m_controlLog;
    
    void logControlCommand(const QString &robotSN, const QJsonObject &command) {
        // 确保日志文件已打开
        if (!m_controlLog.isOpen()) {
            if (!m_controlLog.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
                qDebug() << "无法打开控制命令日志文件：" << m_controlLog.fileName();
                return;
            }
        }
        
        // 创建日志条目
        QJsonObject logEntry;
        logEntry["timestamp"] = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
        logEntry["robot_sn"] = robotSN;
        logEntry["command"] = command;
        
        // 将日志条目转换为JSON字符串并写入文件
        QJsonDocument logDoc(logEntry);
        QByteArray logData = logDoc.toJson(QJsonDocument::Compact) + "\n";
        
        m_controlLog.write(logData);
        m_controlLog.flush(); // 确保立即写入磁盘
        
        qDebug() << QString("控制命令已记录到日志文件：%1").arg(m_controlLog.fileName());
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    
    qDebug() << "=== RobotManager 控制命令测试 ===";
    
    // 创建简化版RobotManager实例
    SimpleRobotManager *robotManager = new SimpleRobotManager();
    
    // 模拟发送控制命令
    QTimer::singleShot(1000, [robotManager]() {
        qDebug() << "测试发送前进命令...";
        
        QVariantMap command;
        command["type"] = "control";
        command["command"] = "forward";
        command["timestamp"] = QDateTime::currentMSecsSinceEpoch();
        
        // 发送命令到机器人GZJY001
        bool success = robotManager->sendControlCommand("GZJY001", command);
        qDebug() << "发送结果:" << (success ? "成功" : "失败");
        
        // 测试其他方向命令
        QTimer::singleShot(1000, [robotManager]() {
            qDebug() << "测试发送左移命令...";
            
            QVariantMap command;
            command["type"] = "control";
            command["command"] = "left";
            command["timestamp"] = QDateTime::currentMSecsSinceEpoch();
            
            bool success = robotManager->sendControlCommand("GZJY001", command);
            qDebug() << "发送结果:" << (success ? "成功" : "失败");
            
            // 检查日志文件是否创建
            QString logDir = QCoreApplication::applicationDirPath() + "/logs";
            QString logFile = logDir + "/control_commands.log";
            QFile file(logFile);
            if (file.exists()) {
                qDebug() << "日志文件已创建:" << logFile;
            } else {
                qDebug() << "日志文件未找到:" << logFile;
            }
            
            // 退出程序
            QTimer::singleShot(1000, []() {
                QCoreApplication::quit();
            });
        });
    });
    
    qDebug() << "测试程序启动，3秒后开始发送控制命令...";
    
    return app.exec();
}

#include "robot_manager_test.moc"